home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 14.0 KB | 520 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UClipboardMgr.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UCLIPBOARDMGR__
- #include "UClipboardMgr.h"
- #endif
-
- // MacApp
-
- #ifndef __UCOREERRORMGR__
- #include "UCoreErrorMgr.h"
- #endif
-
- #ifndef __UCOREGLOBALS__
- #include "UCoreGlobals.h"
- #endif
-
- #ifndef __UCOREUTILITIES__
- #include "UCoreUtilities.h"
- #endif
-
- #ifndef __UDEPENDENCIES__
- #include "UDependencies.h"
- #endif
-
- #ifndef __UDESKSCRAPVIEW__
- #include "UDeskScrapView.h"
- #endif
-
- #ifndef __UDISPATCHER__
- #include "UDispatcher.h"
- #endif
-
- #ifndef __UDOCUMENT__
- #include "UDocument.h"
- #endif
-
- // #ifndef __UERRORMGR__
- // #include "UErrorMgr.h"
- // #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- // #ifndef __UMACAPPUTILITIES__
- // #include "UMacAppUtilities.h"
- // #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UMENUMGR__
- #include "UMenuMgr.h"
- #endif
-
- #ifndef __UVIEWSERVER__
- #include "UViewServer.h"
- #endif
-
- #ifndef __UWINDOW__
- #include "UWindow.h"
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- TClipboardMgr* gClipboardMgr; // declared in interface
-
-
- //========================================================================================
- // CLASS TClipboardMgr
- //========================================================================================
- #undef Inherited
- #define Inherited TBehavior
-
- #pragma segment MAInit
- MA_DEFINE_CLASS_M1(TClipboardMgr, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- TClipboardMgr::TClipboardMgr()
- {
- fClipContext = NULL;
- fClipView = NULL;
- fClipWindow = NULL;
- fClipOrphanage = NULL;
- fGotClipType = FALSE;
-
- fOldScrapStuff.scrapSize = 0;
- fOldScrapStuff.scrapHandle = NULL;
- fOldScrapStuff.scrapCount = 0;
- fOldScrapStuff.scrapState = 0;
- fOldScrapStuff.scrapName = NULL;
-
- fNewScrapStuff = fOldScrapStuff;
- } // TClipboardMgr::TClipboardMgr
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TClipboardMgr::~TClipboardMgr()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::IClipboardMgr:
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void TClipboardMgr::IClipboardMgr()
- {
- this->IBehavior(kClipboardManager);
-
- this->AbsorbScrapStuff(); // get the initial state of the desk scrap
- gClipboardMgr = this;
- } // TClipboardMgr::IClipboardMgr
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::AboutToLoseControl:
- //----------------------------------------------------------------------------------------
- #pragma segment MAActivate
-
- void TClipboardMgr::AboutToLoseControl(Boolean convertClipboard)
- {
- if (convertClipboard)
- {
- this->Changed(mAboutToLoseControl,this);
-
- if ((fClipView != NULL) && (!fClipWrittenToDeskScrap))
- {
- long err = ZeroScrap();
-
- FailInfo fi;
- Try(fi)
- {
- fClipView->WriteToDeskScrap();
- fi.Success();
- }
- else // Recover
- {
- #if qDebugMsg
- fprintf(stderr,"Can't use clipboard data outside this app\n");
- #endif
- if (fi.message == 0)
- fi.message = messageExportClipFailed;
- if (gDispatcher)
- gDispatcher->ShowError(fi.error, fi.message);
- }
- fClipWrittenToDeskScrap = TRUE;
- this->AbsorbScrapStuff();
- }
- }
- } // TClipboardMgr::AboutToLoseControl
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::AbsorbScrapStuff:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- void TClipboardMgr::AbsorbScrapStuff()
- {
- fOldScrapStuff = fNewScrapStuff; // stash previous version, for later
- // change-checkage
- fNewScrapStuff = *(InfoScrap()); // Copy over from low memory to our
- // private global record
- } // TClipboardMgr::AbsorbScrapStuff
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::CheckDeskScrap:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- void TClipboardMgr::CheckDeskScrap()
- {
- OSErr err;
-
- this->AbsorbScrapStuff();
-
- if (fOldScrapStuff.scrapCount != fNewScrapStuff.scrapCount)
- {
- this->Changed(mScrapChanged,this);
-
- fClipView->FreeFromClipboard(); // AbandonCurrentClipboard
- fClipView = NULL; // no reason to have an Undo clipboard
-
- // If the scrap is in memory and we are low on memory, then write
- // the scrap to disk.
- if ((fNewScrapStuff.scrapState > 0) && MemSpaceIsLow())
- err = (OSErr)UnloadScrap(); // Write the scrap to disk.
-
- this->ReadFromDeskScrap();
- }
- } // TClipboardMgr::CheckDeskScrap
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::GetDataToPaste:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- long TClipboardMgr::GetDataToPaste(Handle aDataHandle,
- ResType& dataType)
- {
- long length;
-
- if (fGotClipType)
- {
- dataType = fPrefClipType;
- length = fClipView->GivePasteData(aDataHandle, dataType);
- if (length < 0)
- Failure((OSErr)length, 0);
- }
- else
- {
- length = -1;
-
- #if qDebugMsg
- ProgramBreak("GetDataToPaste called when fGotClipType was false");
- #endif
- }
-
- return length;
- } // TClipboardMgr::GetDataToPaste
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::Launch:
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void TClipboardMgr::Launch()
- {
- // Create the clip window and default view
- FailNIL(fClipWindow = this->MakeClipboardWindow());
- fClipOrphanage = fClipWindow->FindSubView(kIDClipView);
- FailNonObject(fClipOrphanage);
-
- // Add the clipboard manager as an application behavior
- gDispatcher->AddBehavior(this);
-
- this->AbsorbScrapStuff();
- this->ReadFromDeskScrap();
- } // TClipboardMgr::Launch
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::MakeClipboardWindow:
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- TWindow* TClipboardMgr::MakeClipboardWindow()
- {
- TWindow * aWindow;
-
- if (qTemplateViews)
- {
- aWindow = gViewServer->NewTemplateWindow(kIDClipWindow, NULL);
- }
- else
- {
- TDeskScrapView * aDeskScrapView;
-
- aDeskScrapView = new TDeskScrapView;
- aDeskScrapView->IDeskScrapView();
- aDeskScrapView->fIdentifier = kIDClipView;
- aWindow = gViewServer->NewSimpleWindow(kIDClipWindow, TRUE, TRUE, NULL, aDeskScrapView);
- }
-
- if (aWindow)
- {
- aWindow->fHideOnSuspend = TRUE;
- aWindow->fHandlesCursor = FALSE;
- aWindow->fLetsSubViewsHandleCursor = FALSE;
- }
- return aWindow;
- } // TClipboardMgr::MakeClipboardWindow
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::ReadFromDeskScrap:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- void TClipboardMgr::ReadFromDeskScrap()
- {
- MAVolatile(TView*, aViewForClipboard);
-
- FailInfo fi;
- Try(fi)
- {
- aViewForClipboard = this->MakeViewForAlienClipboard();
- fi.Success();
- }
- else // Recover
- {
- aViewForClipboard = fClipOrphanage;
- if (fi.message == 0)
- fi.message = messageImportClipFailed;
- if (gDispatcher)
- gDispatcher->ShowError(fi.error, fi.message);
- }
-
- this->SetClipView(aViewForClipboard,NULL);
- fClipWrittenToDeskScrap = TRUE; // We're in synch with the scrap
- } // TClipboardMgr::ReadFromDeskScrap
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::MakeViewForAlienClipboard:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- TView* TClipboardMgr::MakeViewForAlienClipboard()
- {
- TView * aView;
-
- aView = gDispatcher->MakeViewForAlienClipboard();
- if (!aView)
- aView = fClipOrphanage;
-
- FailNonObject(aView);
- return aView;
- } // TClipboardMgr::MakeViewForAlienClipboard
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::RegainControl:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- void TClipboardMgr::RegainControl(Boolean checkClipboard)
- {
- if (checkClipboard)
- this->CheckDeskScrap();
- } // TClipboardMgr::RegainControl
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::SetClipView:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- void TClipboardMgr::SetClipView(TView* clipView, TCommandHandler* itsContext)
- {
- if (clipView)
- {
- if (fClipWindow)
- {
- // get the old clip window scroller if possible
- TView * theSuperView;
-
- if (fClipWindow->HasSubViews())
- theSuperView = fClipWindow->FirstSubView();
- else
- theSuperView = fClipWindow;
-
- // throw out any views contained in the scroller (or window)
- TView* theSubView;
- while ((theSubView = theSuperView->FirstSubView()) != NULL)
- {
- theSubView->ForceRedraw();
- theSuperView->RemoveSubView(theSubView);
- if (fClipWindow->IsShown())
- theSubView->Close();
- }
-
- // pop in the new clipview
- theSuperView->AddSubView(clipView);
- if (fClipWindow->IsShown())
- clipView->Open();
-
- clipView->AdjustFrame();
- clipView->RevealTop(kDontRedraw);
- clipView->ForceRedraw();
-
- fClipWindow->SetWindowTarget(fClipWindow);
- fClipWrittenToDeskScrap = clipView == fClipOrphanage;
- }
- #if qDebug
- else
- {
- ProgramBreak("SetClipView in absence of fClipWindow");
- }
- #endif
-
- clipView->SetEnable(FALSE); // Ignore clicks while in clipboard views
- this->Changed(mClipViewChanged, this);
- fClipView = clipView;
- fClipContext = itsContext;
-
- if (clipView->fDocument)
- clipView->fDocument->SetIsGhostDocument(TRUE);
- }
- #if qDebug
- else
- {
- ProgramBreak("SetClipView: Attempt to set NULL clipView.");
- }
- #endif
- } // TClipboardMgr::SetClipView
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::DoSetupMenus:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- void TClipboardMgr::DoSetupMenus()
- {
- Inherited::DoSetupMenus();
-
- Enable(cShowClipboard, TRUE);
-
- SetMenuState(cShowClipboard, kIDBuzzString, bzShowClip, bzHideClip, fClipWindow == gDispatcher->GetActiveWindow(kNoFloaters));
-
- } // TClipboardMgr::DoSetupMenus
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::CanPaste:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- void TClipboardMgr::CanPaste(ResType aClipType)
- {
- if (fClipView && !fGotClipType)
- if (fClipView->ContainsClipType(aClipType))
- {
- fGotClipType = TRUE;
- fPrefClipType = aClipType;
- }
-
- if (!gDispatcher->fSysWindowActive)
- Enable(cPaste, fGotClipType);
- } // TClipboardMgr::CanPaste
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::DoMenuCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- void TClipboardMgr::DoMenuCommand(CommandNumber aCommandNumber)
- {
- switch (aCommandNumber)
- {
- case cShowClipboard:
- if (fClipWindow == gDispatcher->GetActiveWindow(kNoFloaters))
- fClipWindow->CloseAndFree(); // Hide the clipboard
- else
- {
- fClipWindow->Open();
- fClipWindow->Select();
- }
- break;
- default:
- Inherited::DoMenuCommand(aCommandNumber);
- break;
- }
- } // TClipboardMgr::DoMenuCommand
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::Close:
- //----------------------------------------------------------------------------------------
- #pragma segment MATerminate
-
- void TClipboardMgr::Close()
- {
- LoadScrap();
- } // TClipboardMgr::Close
-
- //----------------------------------------------------------------------------------------
- // TClipboardMgr::PutDeskScrapData:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- OSErr TClipboardMgr::PutDeskScrapData(ResType aResType,
- Handle aDataHandle)
- {
- SignedByte savedState = LockHandle(aDataHandle);
- long err = PutScrap(GetHandleSize(aDataHandle), aResType, (*aDataHandle));
- HSetState(aDataHandle, savedState);
- #if qDebugMsg
- if (err != noErr)
- fprintf(stderr,"Error from PutScrap is: %d\n", err);
- #endif
- return err;
- } // TClipboardMgr::PutDeskScrapData
-
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // InitUClipboardMgr:
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void InitUClipboardMgr()
- {
- TClipboardMgr * aClipboardMgr;
-
- aClipboardMgr = new TClipboardMgr;
- aClipboardMgr->IClipboardMgr();
-
- #if qTemplateViews
- MA_REGISTER_CLASS(TDeskScrapView);
- #endif
-
- } // InitUClipboardMgr
-
- //----------------------------------------------------------------------------------------
- // End of UClipboardMgr.cp
-
- #pragma segment Inline
-